package io.pumelo.property;


import io.pumelo.Pumelo;
import io.pumelo.tools.HttpClient;

import java.util.ArrayList;
import java.util.List;


/**
 * Created by Studio on 2017/6/28.
 */
public class RemoteProperties {

    private String url = "http://127.0.0.1/application.properties";

    private List<String> properties = new ArrayList<String>();

    private Pumelo context;

    public RemoteProperties(Pumelo context) {
        this.context = context;
    }

    public String[] LoadProperty(String version, String prefix) {
        //加载远程配置
        String content = HttpClient.sendGet(url,"");

        if(Pumelo.isShowLogs())
        {
            System.out.println("===========================Raw Properties===========================");
            System.out.println(content);
            System.out.println("========================Converted Properties========================");
        }

        String[] lines = content.split("\n");
        if(lines.length == 0)
        {
            return null;
        }


        for (String line : lines) {

            if (line == null || line.trim().length() == 0) {
                continue;
            }
            if(line.trim().toCharArray()[0] == '#')
            {
                continue;
            }
            if(line.split("=").length!=2)
            {
                continue;
            }

            properties.add(prefix+line.trim());

            if(Pumelo.isShowLogs()) {
                System.out.print(prefix);
                System.out.print(line.trim().split("=")[0]);
                System.out.print("=");
                System.out.print(line.trim().split("=")[1]);
                System.out.print("\n");
            }

        }


        return (String[]) properties.toArray(new String[properties.size()]);
    }
}
